<?php

namespace Task\Controller;

use Common\Controller\BaseController;
use Think\Page;

class ScoreController extends BaseController
{
    public function index()
    {
        $get_task_type = $_GET['task_type'];
        $model = D('Task');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_POST['search_key'];
        $status = 1;

        $task_type = $_POST['task_type'];

        if (is_numeric($get_task_type)) {
            $task_type = $get_task_type;
        }

        $this->assign('task_type', $task_type);

        $user_id = $this->current_user['id'];

        $list = $model->get_list($search_key, $status, $task_type, $sp, C('PAGE_SIZE'), true);

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('task_list', $list[0]);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }

        $this->display();
    }

    public function detail()
    {
        $taskid = $_GET['taskid'];
        $s_status = $_GET['search_status'];
        if (empty($taskid)) {
            $taskid = $_POST['taskid'];
        }
        $search_key = $_POST['search_key'];
        $search_status = $_POST['search_status'];

        $rank = $_POST['rank'];
        $attribute = $_POST['attribute'];

        if ($search_status != '') {
            $status = explode(",", $search_status);
        } else {
            if (!empty($s_status)) {
                $status = $s_status;
            }
        }

        $model = D('Task');
        $task = $model->get($taskid);
        $this->assign('task', $task);

        $model = D('VUserTask');
        $list = $model->get_list($user_id, $search_key, $status, $task_type, $taskid, $rank, $attribute, $sp, 0);

        $page = new Page($list[1], C('PAGE_SIZE'));
        $this->assign('user_task_list', $list[0]);
        $this->assign('_page', $page);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }

        $this->display();
    }

    public function save()
    {
        $task_type = $_POST['task_type'];
        $model = D('UserTask');
        $data = $model->create();
        if ($data['status'] == 2) {
            $data['report_at'] = time();
        }
        $result = $model->save($data);

        if (!$result && $result != '') {
            echo $model->getError();
        } else {
            $annex_name = $_POST['annex_name'];
            $annex_path = $_POST['annex_path'];
            $contactphone = $_POST['contactphone'];
            $contactpretel = $_POST['contactpretel'];
            $contacttel = $_POST['contacttel'];
            $cmodel = D('UserTaskAnnex');
            for ($i = 0; $i < count($annex_name); $i++) {
                if (!empty($annex_name[$i])) {
                    $cdata = $cmodel->create();
                    $name = $annex_name[$i];
                    $ename = end(explode(".", $name));
                    $atype = 0;
                    if (strtolower($ename) == 'doc') {
                        $atype = 1;
                    } else {
                        $atype = 2;
                    }
                    $cdata['id'] = null;
                    $cdata['annex_name'] = $name;
                    $cdata['annex_path'] = $annex_path[$i];
                    $cdata['annex_type'] = $atype;
                    $cdata['extension'] = $ename;
                    $cdata['user_task_id'] = $data['id'];
                    $cmodel->add($cdata);
                }
            }
        }
        $this->redirect('task/index/index', array('task_type' => $task_type));
    }

    public function assess()
    {
        $task_type = $_POST['task_type'];
        $model = D('UserTask');

        $amount_score = $_POST['amount_score'];
        $user_task_id = $_POST['user_task_id'];
        $remark = $_POST['remark'];

        for ($i = 0; $i < count($amount_score); $i++) {
            if (!empty($amount_score[$i])) {
                $data = array('amount_score' => $amount_score[$i], 'remark' => $remark[$i], 'status' => 3);
                $model->where('id=%d', $user_task_id[$i])->setField($data);
            }
        }
        redirect('/task/score/index?task_type=' . $task_type);
//        $this->redirect('task/score/index', array('task_type' => $task_type));
    }

    public function back()
    {
        $model = D('UserTask');

        $user_task_id = $_POST['user_task_id'];
        $back_reason = $_POST['back_reason'];
        $data = array('amount_score' => 0, 'remark' => '','back_reason' => $back_reason, 'is_back' => 1, 'status' => 0);
        $model->where('id=%d', $user_task_id)->setField($data);

        echo '0';
        exit();
    }

    public function deleteannex()
    {
        $id = $_POST['id'];

        $usertaskannexmodel = D('UserTaskAnnex');

        $result = $usertaskannexmodel->delete($id);
        if (!$result && $result != '') {
            echo $result->getError();
        } else {
            echo 0;
        }
    }
}
